﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Locations
{
    /// <summary>
    ///  位置服务基类
    /// </summary>
    public class BaseLocation
    {
        private const double EARTH_RADIUS = 6378.137;

        #region 计算两个位置之间的距离
        /// <summary>
        /// 计算两个位置之间的距离 
        /// </summary>
        /// <param name="location1"></param>
        /// <param name="location2"></param>
        /// <returns></returns>
        public double GetDistance(LocationPoint location1, LocationPoint location2)
        {
            double radLat1 = rad(location1.lat);
            double radLat2 = rad(location2.lat);
            double a = radLat1 - radLat2;
            double b = rad(location1.lng) - rad(location2.lng);
            double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
                                             Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
            s = s * EARTH_RADIUS;
            s = Math.Round(s * 10000) / 10000;
            return s;
        }

        private double rad(double d)
        {
            return d * Math.PI / 180.0;
        }
        #endregion

    }
}
